using UnityEngine;

public class CrowdSetter : MonoBehaviour
{
	public struct VerticeSetup
	{
		public int group;

		public bool hasGroup;
	}

	public int numSeats = 10;

	public int numPlacesInTexture = 10;

	private void Start()
	{
		Mesh mesh = GetComponent<MeshFilter>().mesh;
	}

	public void UpdateRandomOnShared()
	{
		SetRandomCrowd(GetComponent<MeshFilter>().sharedMesh);
	}

	private void SetRandomCrowd(Mesh mesh)
	{
		Vector2[] uv = mesh.uv;
		if (mesh.uv2 == null || mesh.uv2.Length != uv.Length)
		{
			Vector2[] array = new Vector2[uv.Length];
			for (int i = 0; i < uv.Length; i++)
			{
				array[i] = uv[i];
			}
			mesh.uv2 = array;
		}
		Vector2[] uv2 = mesh.uv2;
		Vector3[] vertices = mesh.vertices;
		VerticeSetup[] array2 = new VerticeSetup[uv2.Length];
		int[] triangles = mesh.triangles;
		int num = 0;
		for (int j = 0; j < triangles.Length / 3; j++)
		{
			int group = num;
			bool flag = false;
			for (int k = 0; k < 3; k++)
			{
				int num2 = triangles[j * 3 + k];
				VerticeSetup verticeSetup = array2[num2];
				if (verticeSetup.hasGroup)
				{
					flag = true;
					group = verticeSetup.group;
					break;
				}
			}
			if (!flag)
			{
				for (int l = 0; l < 3; l++)
				{
					int num3 = triangles[j * 3 + l];
					Vector3 vector = vertices[num3];
					Vector2 vector2 = uv2[num3];
					for (int m = 0; m < triangles.Length / 3; m++)
					{
						for (int n = 0; n < 3; n++)
						{
							int num4 = triangles[m * 3 + n];
							Vector3 vector3 = vertices[num4];
							Vector2 vector4 = uv2[num4];
							VerticeSetup verticeSetup2 = array2[num4];
							if (verticeSetup2.hasGroup && vector2 == vector4 && vector == vector3)
							{
								flag = true;
								group = verticeSetup2.group;
								break;
							}
						}
					}
					if (flag)
					{
						break;
					}
				}
			}
			for (int num5 = 0; num5 < 3; num5++)
			{
				int num6 = triangles[j * 3 + num5];
				array2[num6].hasGroup = true;
				array2[num6].group = group;
			}
			if (!flag)
			{
				num++;
			}
		}
		Vector2[] array3 = new Vector2[uv2.Length];
		int num7 = 0;
		int[] array4 = new int[num];
		for (int num8 = 0; num8 < array4.Length; num8++)
		{
			array4[num8] = Random.Range(0, numSeats);
		}
		float num9 = 1f / (float)numPlacesInTexture;
		for (int num10 = 0; num10 < uv2.Length; num10++)
		{
			num7 = array4[array2[num10].group];
			Vector2 vector5 = uv2[num10];
			if (vector5.x < 0.05f)
			{
				vector5.x = num9 * (float)num7;
			}
			else
			{
				vector5.x = num9 * (float)(num7 + 1);
			}
			array3[num10] = vector5;
		}
		mesh.uv = array3;
	}
}
